package com.ryder.book_cycle.controller;

import com.ryder.book_cycle.common.response.Result;
import com.ryder.book_cycle.entity.HomeInfoDTO;
import com.ryder.book_cycle.entity.UserAccount;
import com.ryder.book_cycle.entity.UserAccountResult;
import com.ryder.book_cycle.mapper.MerchantProfileMapper;
import com.ryder.book_cycle.service.UserAccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.Map;

/**
 * 描述: 用户账号表(UserAccount)表控制层
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-07-08 16:40:45
 */
@RestController
@RequestMapping("/userAccount")
public class UserAccountController {

    private static final Logger log = LoggerFactory.getLogger(UserAccountController.class);

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 登录
     */
    @PostMapping("/doLogin")
    public Result<UserAccountResult> doLogin(@RequestBody UserAccount entity) {
        try {
            QueryWrapper<UserAccount> wrapper = new QueryWrapper<>(entity);
            UserAccount one = userAccountService.getOne(wrapper);
            if (one == null) {
                return Result.fail("账号不存在");
            }

            UserAccountResult userAccountResult = new UserAccountResult();
            userAccountResult.setUserType(one.getUserType());
            userAccountResult.setUserId(one.getId());

            if (one.getUserType() == 0) {
                return Result.ok(userAccountResult);
            }

            boolean isMerchant = userAccountService.existsMerchantId(one.getId());
            if (!isMerchant) {
                return Result.fail("账号不存在");
            }

            userAccountResult.setUserId(one.getId());
            return Result.ok(userAccountResult);
        } catch (Exception e) {
            log.error("登录异常：{}", e.getMessage(), e);
            return Result.fail("登录异常");
        }
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<Page<UserAccount>> pageList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<UserAccount> page = new Page<>(pageNum, pageSize);
            QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
            Page<UserAccount> result = userAccountService.page(page, wrapper);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("分页查询失败：{}", e.getMessage(), e);
            return Result.fail("分页查询异常");
        }
    }

    /**
     * 根据主键查询
     */
    @GetMapping("/{id}")
    public Result<UserAccount> queryById(@PathVariable Long id) {
        try {
            UserAccount entity = userAccountService.getById(id);
            return entity != null ? Result.ok(entity) : Result.fail("未找到数据");
        } catch (Exception e) {
            log.error("查询失败：{}", e.getMessage(), e);
            return Result.fail("查询异常");
        }
    }

    @GetMapping("/home/{id}")
    public Result<HomeInfoDTO> queryHomeInfoById(@PathVariable Long id) {
        try {
            HomeInfoDTO entity = userAccountService.queryHomeInfoById(id);
            return entity != null ? Result.ok(entity) : Result.fail("未找到数据");
        } catch (Exception e) {
            log.error("查询失败：{}", e.getMessage(), e);
            return Result.fail("查询异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody UserAccount entity) {
        try {
            boolean saved = userAccountService.add(entity);
            return saved ? Result.ok(true) : Result.fail("新增失败");
        } catch (Exception e) {
            log.error("新增失败：{}", e.getMessage(), e);
            return Result.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody UserAccount entity) {
        try {
            boolean updated = userAccountService.updateById(entity);
            return updated ? Result.ok(true) : Result.fail("更新失败");
        } catch (Exception e) {
            log.error("更新失败：{}", e.getMessage(), e);
            return Result.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            boolean removed = userAccountService.removeById(id);
            return removed ? Result.ok(true) : Result.fail("删除失败");
        } catch (Exception e) {
            log.error("删除失败：{}", e.getMessage(), e);
            return Result.fail("删除异常");
        }
    }

}
